﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static System.Collections.Specialized.BitVector32;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 界平面
    /// </summary>
    /// <remarks>对 SketchUpAPI 中的 SUSectionPlaneRef 的包装</remarks>
    public class SectionPlane : DrawingElement
    {
        #region API 函数
        /// <summary>
        /// 创建截面
        /// </summary>
        /// <param name="sectionPlane"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUSectionPlaneCreate(out IntPtr sectionPlane);

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="sectionPlane"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUSectionPlaneRelease(ref IntPtr sectionPlane);

        /// <summary>
        /// 设置截面位置
        /// </summary>
        /// <param name="sectionPlane"></param>
        /// <param name="plane"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUSectionPlaneSetPlane(IntPtr sectionPlane, Plane3D plane);

        /// <summary>
        /// 获取截面位置
        /// </summary>
        /// <param name="sectionPlane"></param>
        /// <param name="plane"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUSectionPlaneGetPlane(IntPtr sectionPlane, ref Plane3D plane);

        /// <summary>
        /// 是否激活
        /// </summary>
        /// <param name="sectionPlane"></param>
        /// <param name="is_active"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUSectionPlaneIsActive(IntPtr sectionPlane, out bool is_active);

        /// <summary>
        /// 获取截面名字
        /// </summary>
        /// <param name="sectionPlane"></param>
        /// <param name="name"><see cref="StringRef"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUSectionPlaneGetName(IntPtr sectionPlane, ref IntPtr name);

        /// <summary>
        /// 设置截面名字
        /// </summary>
        /// <param name="sectionPlane"></param>
        /// <param name=""></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUSectionPlaneSetName(IntPtr sectionPlane, [MarshalAs(UnmanagedType.LPUTF8Str)] string name);

        /// <summary>
        /// 获取截面符号
        /// </summary>
        /// <param name="sectionPlane"></param>
        /// <param name="symbol"><see cref="StringRef"/> 符号</param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUSectionPlaneGetSymbol(IntPtr sectionPlane, ref IntPtr symbol);

        /// <summary>
        /// 设置截面符号
        /// </summary>
        /// <param name="sectionPlane"></param>
        /// <param name="symbol"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUSectionPlaneSetSymbol(IntPtr sectionPlane, [MarshalAs(UnmanagedType.LPUTF8Str)] string symbol);
        #endregion

        /// <summary>
        /// 截面名字
        /// </summary>
        public string Name
        {
            get
            {
                StringRef s = StringRef.Empty;
                SketchUpExceptionHelper.ThrowSUResult(SUSectionPlaneGetName(Id, ref s.Id));
                return s.Dispose();
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUSectionPlaneSetName(Id, value));
            }
        }

        /// <summary>
        /// 截面符号
        /// </summary>
        public string Symbol
        {
            get
            {
                StringRef s = StringRef.Empty;
                SketchUpExceptionHelper.ThrowSUResult(SUSectionPlaneGetSymbol(Id, ref s.Id));
                return s.Dispose();
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUSectionPlaneSetSymbol(Id, value));
            }
        }

        /// <summary>
        /// 是否是激活状态
        /// </summary>
        public bool IsActive
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUSectionPlaneIsActive(Id, out bool active));
                return active;
            }
        }

        /// <summary>
        /// 截面平面
        /// </summary>
        public Plane3D Plane
        {
            get
            {
                Plane3D p = new Plane3D();
                SketchUpExceptionHelper.ThrowSUResult(SUSectionPlaneGetPlane(Id, ref p));
                return p;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUSectionPlaneSetPlane(Id, value));
            }
        }

        /// <summary>
        /// 创建一个截平面
        /// </summary>
        public SectionPlane()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUSectionPlaneCreate(out Id));
        }

        internal SectionPlane(IntPtr id) => Id = id;

        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUSectionPlaneRelease(ref Id));
        }
    }
}